﻿namespace EFS.RC.CCAdmin.Surveys.Question
{
	/// Class Edit
	public partial class Edit : EFS.RC.CCAdmin.Common.Control
	{
		#region Locals
		/// host
		private EFS.Common.UI.Controls.ModalDialogHost host = null;

		/// category ID
		private System.Int32 categoryId = 1;

		/// answer orientation direction

		public System.Int32 answersflowdirection = 1;
		#endregion

		#region Constructors
		/// Implicit
		public Edit()
		{
			InitializeComponent();
		}

		/// Explicit
		public Edit(
			EFS.Common.UI.Controls.ModalDialogHost host,
			System.Int32 categoryId,
			System.Int32? questionID)
		{
			// check
			EFS.Common.Verify.Reference(host, "host");

			InitializeComponent();

			//set listener up
			UtilitiesService.SurveyQuestion_GetCompleted += UtilitiesService_SurveyQuestion_GetCompleted;

			//set new question response listener
			UtilitiesService.SurveyQuestion_CreateCompleted += UtilitiesService_SurveyQuestion_CreateCompleted;

			//set edit response listener
			UtilitiesService.SurveyQuestion_UpdateCompleted += UtilitiesService_SurveyQuestion_UpdateCompleted;

			this.host = host;
			this.host.OnButtonOKClick += OnParentOKClicked;

			// set ID
			this.QuestionID = questionID;
			this.categoryId = categoryId;
		}
		#endregion

		#region Properties
		/// Property QuestionID: Get, Set
		public System.Int32? QuestionID { get; set;}

		/// Property IsValid: Get
		private System.Boolean IsValid
		{
			get
			{
				return EFS.Common.Verify.String(QuestionName) && Priority.HasValue;
			}
		}

		/// Property IsValid: Get
		private System.String QuestionName
		{
			get
			{
				return EFS.Common.Functions.String.GetTrimmedString(textBoxQuestionName.Text);
			}
		}

		/// Property IsValid: Priority
		private System.Int32? Priority
		{
			get
			{
				System.Int32 retValue = 0;
				if (System.Int32.TryParse(textBoxPriority.Text, out retValue))
				{
					return retValue;
				}

				// none
				return null;
			}
		}
		#endregion

		#region LoadData
		/// Load data
		private void LoadData()
		{
			// if edit
			if (QuestionID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while retrieving data");

				// call ws
				UtilitiesService.SurveyQuestion_GetAsync(QuestionID.Value);

				surveyAnswersList.Visibility = System.Windows.Visibility.Visible;
				return;
			}

			surveyAnswersList.Visibility = System.Windows.Visibility.Collapsed;
		}
		#endregion

		#region WS Callbacks
		/// GetQuestionDetailsCompleted
		private void UtilitiesService_SurveyQuestion_GetCompleted(object sender, WebProxy.SurveyQuestion_GetCompletedEventArgs e)
		{
			// close wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// deserialize
				EFS.RC.CC.CH.Entities.Surveys.Question question =
					EFS.Common.Serialization.Functions.Deserialize<EFS.RC.CC.CH.Entities.Surveys.Question>(e.Result.ToString());

				//bind/fill form
				textBoxQuestionName.Text = question.Text;
				textBoxPriority.Text = question.Priority.HasValue ? question.Priority.Value.ToString() : "";

				//bind answerflow
				if (question.AnswersFlowDirection.HasValue)
				{
					if (((System.Int32)(question.AnswersFlowDirection)) == 1)
					{
						VerticalButtonAlign.IsChecked = true;
					
					}
					else
					{
						HorizontalButtonAlign.IsChecked = true;
					
					}
					 
				}

				
				// load list
				surveyAnswersList.SetQuesionId(QuestionID);
			}
			catch (System.Exception ex)
			{
				ShowErrorMessage("Failed to retrieve question. " + ex.Message);
			}
		}

		/// Create question completed
		private void UtilitiesService_SurveyQuestion_CreateCompleted(object sender, WebProxy.SurveyQuestion_CreateCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				this.QuestionID = e.Result;
				System.Windows.MessageBox.Show("Question successfully created. You may now add some answers.");
				LoadData();
			}
			catch (System.Exception)
			{
				ShowErrorMessage("Failed to create question.");
			}
		}

		/// Update question completed
		private void UtilitiesService_SurveyQuestion_UpdateCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
		{
			// show wait DLG
			EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.Completed(this);

			try
			{
				// verify
				if (null != e.Error)
				{
					throw e.Error;
				}

				// close parent
				this.host.Close(EFS.Common.UI.Controls.ModalDialogHost.ClosedReason.Ok);
			}
			catch (System.Exception)
			{
				ShowErrorMessage("Failed to update question.");
			}
		}
		#endregion

		#region Events
		/// OnParentOKClicked
		private void OnParentOKClicked(object sender, System.Windows.RoutedEventArgs e, System.Boolean dialogResult)
		{
			// test if valid
			if (!IsValid)
			{
				return;
			}

			// create
			if (!QuestionID.HasValue)
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while creating question");

				//ws call
				UtilitiesService.SurveyQuestion_CreateAsync(categoryId, QuestionName, Priority.Value,answersflowdirection);
			}
			else
			{
				// show wait DLG
				EFS.Common.UI.Controls.NotificationDialog.ProgressDialog.InProgress(this, "Please wait while updating question");

				//ws call
				UtilitiesService.SurveyQuestion_UpdateAsync(QuestionID.Value, categoryId, QuestionName, Priority.Value,answersflowdirection);
			}
		}

		/// Loaded
		private void Control_Loaded(object sender, System.Windows.RoutedEventArgs e)
		{
			// ignore in design tool
			if (!System.ComponentModel.DesignerProperties.IsInDesignTool)
			{
				//load data
				LoadData();
			}
		}
		#endregion

		

		private void HorizontalButtonAlign_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			answersflowdirection = 2;
		}

		private void VerticalButtonAlign_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			answersflowdirection = 1;
		}

	 
	}

}